# encoding=utf8
import json
import re
import time
import math
import codecs
from pymongo import MongoClient
from bson import json_util
from urllib.parse import quote_plus
from tqdm import tqdm


user = 'mongouser'
pwd = 'tuanjie123'
host = '118.25.106.14'
port = 27017
url = 'mongodb://%s:%s@%s' % (quote_plus(user), quote_plus(pwd), host)

client = MongoClient(url, port=port)
collection_task_test = client['tagging_test']['task_test']

date_list = [str(i) for i in range(1, 16)]

for idx in range(len(date_list)-1):
    lines = collection_task_test.find(
        {
            'taskKey': 'sentimentAnalysis',
            'canOutput': 1,
            'checkDate': {
                '$gte': time.mktime(time.strptime("2022-7-{} 0:0:0".format(date_list[idx]), "%Y-%m-%d %H:%M:%S")) * 1000,
                '$lt': time.mktime(time.strptime("2022-7-{} 0:0:0".format(date_list[idx + 1]), "%Y-%m-%d %H:%M:%S")) * 1000
            }
        },
        {
            'taskData': 1,
            'checkData': 1,
            'reCheckData': 1,
            'finalData': 1,
            '_id': 0,
        },
        no_cursor_timeout=True
    )

    # raise Exception('ting')
    total = lines.count()
    # raise Exception("ting")

    save_list = []
    success_count = 0
    error_count = 0

    for line in tqdm(lines, total=total):  # lines是动态读取mongo中的数据
        save_list.append(line)

        # save_list.append(line)
        success_count += 1

    # 判断是否有多余数据
    if save_list:
        with codecs.open('./emotion_json_file_6月份/{}'.format(date_list[idx]) + '.json', 'w', encoding='utf-8') as f:
            json.dump(save_list, f, ensure_ascii=False, indent=4)

    print('2022-7-{}: {}'.format(date_list[idx], success_count))
    print(error_count)

